﻿using MVC_Project.BLL;
using Project.DAL;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.BLL
{
    public class MenusBll : BaseBll<Menus>
    {
        public override void SetDal()
        {
            Dal = new MenusDal();
        }

        public List<Menus> GetMenus() {
            return base.Query().ToList();
        }

        public List<Menus> GetMenusByPage(string name, int page, int size, out int count)
        {
            var menus = base.Query();

            if (!string.IsNullOrWhiteSpace(name))
            {
                menus = menus.Where(m => m.MName.Contains(name));
            }
            count = menus.Count();

            var data = menus.OrderBy(m => m.MId).Skip((page - 1) * size).Take(size);

            return data.ToList();
        }

        public List<Menus> GetFu()
        {
            var data = Dal.Query().ToList();

            var list = data.Where(m => m.ParentId == 0);

            var fu = from m in list
                     select new Menus
                     {
                         MId = m.MId,
                         MName = m.MName,
                     };

            return fu.ToList();
        }

        public bool DeleteMenus(int MId)
        {
            var meuns = Dal.Query().Where(u => u.MId == MId).FirstOrDefault();
            if (meuns != null)
            {
                Dal.Delete(meuns);
                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }

        public bool DeleteMenusAll(int[] MId)
        {
            var meuns = Dal.Query().Where(u => MId.Contains(u.MId)).ToList();
            if (meuns != null)
            {
                Dal.Delete(meuns);
                return Dal.SaveChanges();
            }
            else
            {
                return false;
            }
        }


    }
}
